﻿
//Author: 张亚涛
//createtime:2015-12-26 22:36:56
//Description:

using System;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using NS.DAL.Systems;
using Model;
using DAL;

namespace DAL
{
    /// <summary>
    /// 该类提供了一系列操作 "UaerCom" 表的方法;
    /// </summary>
    public class UaerComBase
    {
        const string SQL_Select = "select [ID],[UserID],[CommuntyID] from [UaerCom] ";
        #region .对本表的维护.
        #region select data
        /// <summary>
        /// 返回UaerCom表的所有数据  
        /// </summary>
        /// <param name="UserID">员工ID</param>
        /// <returns></returns>        
        public static List<UaerCom> GetAllUaerCom(long UserID)
        {
            List<UaerCom> uaerComList = new List<UaerCom>();
            SqlDataReader reader = null;

            string sql = SQL_Select + " where UserID='" + UserID + "'";
            reader = SQLHelper.ExecuteDataReader(sql, null);
            while (reader.Read())
            {
                UaerCom uaerCom = new UaerCom(reader);
                uaerComList.Add(uaerCom);
            }
            reader.Close();
            return uaerComList;
        }

        /// <summary>
        /// 根据小区ID返回数据列表
        /// </summary>
        /// <param name="CommuntyID">小区ID</param>
        /// <returns></returns>        
        public static List<UaerCom> GeListByCommuntyID(long CommuntyID)
        {
            List<UaerCom> uaerComList = new List<UaerCom>();
            SqlDataReader reader = null;

            string sql = SQL_Select + " where CommuntyID='" + CommuntyID + "'";
            reader = SQLHelper.ExecuteDataReader(sql, null);
            while (reader.Read())
            {
                UaerCom uaerCom = new UaerCom(reader);
                uaerComList.Add(uaerCom);
            }
            reader.Close();
            return uaerComList;
        }

        /// <summary>
        /// 返回空闲员工数量
        /// </summary>
        /// <returns></returns>        
        public static int GetUserCount()
        {
            int res = 0;
            string sql = "select count(UserID) from [UserDetail] where IsTrader=1 and UserID not in (select UserID from [Order] where State=2)";
            res=int.Parse(SQLHelper.ExecuteScalar(sql, null));
            return res;
        }

        /// <summary>
        /// 返回员工负责小区ID
        /// </summary>
        /// <param name="WeChat">员工微信号</param>
        /// <returns></returns>        
        public static string GetComInStr(string WeChat)
        {
            string resStr = string.Empty;
            string sql = "select [CommuntyID] from [UaerCom] where UserID=(select UserID from UserDetail where WeChat='" + WeChat + "')";
            SqlDataReader reader = SQLHelper.ExecuteDataReader(sql, null);
            while (reader.Read())
            {
                resStr += reader[0].ToString() + ",";
            }
            reader.Close();
            if (!string.IsNullOrEmpty(resStr))
            {
                resStr = resStr.Substring(0, resStr.Length - 1);
            }
            return resStr;
        }

        /// <summary>
        /// 根据UaerCom表的ID字段返回数据 
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns></returns>       
        public static UaerCom GetUaerComById(int id)
        {
            UaerCom uaerCom = null;
            SqlDataReader reader = null;

            string sql = SQL_Select + "where [ID] = @Id";
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@Id",id)
            };
            reader = SQLHelper.ExecuteDataReader(sql, paras);
            if (reader.Read())
            {
                uaerCom = new UaerCom(reader);
            }
            reader.Close();
            return uaerCom;
        }
        #endregion
        #region delete data
        /// <summary>
        /// 根据UaerCom表的ID字段删除数据 
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>        
        public static int DeleteUaerComById(int id)
        {
            string sql = "delete from [UaerCom] where [ID] = @Id";
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@Id",id)
            };
            return SQLHelper.ExecuteNonQuery(sql, paras);
        }
        #endregion
        #region update data
        /// <summary>
        /// prepare parameters 
        /// </summary>
        public static SqlParameter[] PrepareCommandParameters(UaerCom uaerCom)
        {
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@Id",uaerCom.Id),
            new SqlParameter("@UserID",uaerCom.UserID),
            new SqlParameter("@CommuntyID",uaerCom.CommuntyID)    
            };
            return paras;
        }
        /// <summary>
        /// 根据UaerCom表的ID字段更新数据 
        /// </summary> 
        /// <param name="UaerCom">uaerCom</param>
        /// <returns></returns>       
        public static int UpdateUaerComById(UaerCom uaerCom)
        {
            string sql = "update [UaerCom] set [UserID] = @UserID,[CommuntyID] = @CommuntyID where [ID] = @Id";
            SqlParameter[] paras = PrepareCommandParameters(uaerCom);
            return SQLHelper.ExecuteNonQuery(sql, paras);
        }
        #endregion
        #region insert data
        /// <summary>
        /// 向UaerCom表插入一条数据，返回自增列数值，插入不成功则返回-1
        /// </summary>
        /// <param name="uaerCom">UaerCom</param>       
        /// <returns></returns>        
        public static int AddUaerCom(UaerCom uaerCom)
        {
            string sql = "insert into [UaerCom]([UserID],[CommuntyID])values(@UserID,@CommuntyID)    select @@identity";
            SqlParameter[] paras = PrepareCommandParameters(uaerCom);
            int id = -1;

            string _ID = SQLHelper.ExecuteScalar(sql, paras).ToString();
            int.TryParse(_ID, out id);

            return id;
        }
        #endregion
        #endregion

        #region .SQL语句汇总.
        /// <summary>
        /// 返回UaerCom表的所有数据 （SQL语句）
        /// </summary>
        /// <returns></returns>        
        public static string GetAllUaerCom_SQL()
        {
            return SQL_Select;
        }
        #endregion
    }
}